﻿/// <reference path="/js/jquery-2.1.3.min.js" />
/// <reference path="/js/knockout-3.2.0.debug.js" />

function Alumini(data) {
    this.FirstName = ko.observable(data.FirstName);
    this.LastName = ko.observable(data.LastName);
    this.UserID = ko.observable(data.UserID);
    this.Age = ko.observable(data.Age);
    this.Gender = ko.observable(data.Gender);
    this.ClassID = ko.observable(data.ClassID);
    this.StreamID = ko.observable(parseInt( data.DeptID));
    this.Stream = ko.observable(data.Stream);
    this.ImagePath=ko.observable("/Handler.ashx?ID="+data.UserID);
    this.PassingYear = ko.observable(parseInt(data.PassingYear));
      this.FullName = ko.computed(function() {
        return this.FirstName() + " " + this.LastName();
    }, this);
      this.ContactURL = ko.observable("/Alumini/ALM-Compose.aspx?UserID="+data.UserID);
}

function BindYearArray(data){
    this.Batch = ko.observable(data.Name);
    this.PassingYear = ko.observable(data.Age);
}

$(document).ready(function () {
    ko.applyBindings(new DropDownModel());
});

function DropDownModel() {
    var self = this;
    self.Streams = ko.observableArray([]);
    self.YearRange = ko.observableArray([{ 'Batch': '2010-2014', 'PassingYear':2014}, { 'Batch': '2009-2013', 'PassingYear':2013}
        , { 'Batch': '2008-2012', 'PassingYear':2012}, { 'Batch': '2007-2011', 'PassingYear':2011}, { 'Batch': '2006-2012', 'PassingYear':2010}
        , { 'Batch': '2005-2009', 'PassingYear':2009}, { 'Batch': '2004-2008', 'PassingYear':2008}, { 'Batch': '2003-2007', 'PassingYear':2007}
    ]);
    self.Aluminis = ko.observableArray([]);
    self.AluminiList={
        PassingYear: ko.observable(null),
        SelectedStream : ko.observable(null)
    };
    self.BindSelectedStream = function (fields) {
        if (ko.toJSON(self.AluminiList.SelectedStream()) == ko.toJSON(fields.StreamID))
            self.AluminiList.SelectedStream(null);
        else
            self.AluminiList.SelectedStream(fields.StreamID);
        self.filterAlumini();
     
    };
    self.AssignSelectedYear = function (fields) {
        if (ko.toJSON(self.AluminiList.PassingYear()) == fields.PassingYear)
            self.AluminiList.PassingYear(null);
        else
            self.AluminiList.PassingYear(fields.PassingYear);
        self.filterAlumini();
     
     };

     self.BindAlumini = function () {
         var data = ko.toJSON({ data: self.AluminiList })
         $.ajax({
             type: "POST",
             url: '/WebServices/DataService.asmx/Bind_AluminiPics',
             data: data,
             contentType: "application/json; charset=utf-8",
             async:false,
             success: function (data) {
                 var Alms = $.map(data.d, function (item) {
                     return new Alumini(item)
                 });
              return   self.Aluminis(Alms);
             },
             error: function (err) {
                 alert(err.status + " - " + err.statusText);
             }
         });
     }

     self.filterAlumini = ko.computed(function () {
         if (!self.AluminiList.PassingYear() && !self.AluminiList.SelectedStream()) {
             if (ko.toJSON(self.Aluminis).length <=2)
             self.BindAlumini();
             return self.Aluminis();
         } else {
             return ko.utils.arrayFilter(self.Aluminis(), function (alm) {
                 if (self.AluminiList.PassingYear() && self.AluminiList.SelectedStream()) {
                     return ko.toJSON(alm.PassingYear) == ko.toJSON(self.AluminiList.PassingYear()) && ko.toJSON(alm.StreamID) == ko.toJSON(self.AluminiList.SelectedStream());
                 }
                 else if (!self.AluminiList.PassingYear() && self.AluminiList.SelectedStream()) {
                     return ko.toJSON(alm.StreamID) == ko.toJSON(self.AluminiList.SelectedStream());
                 }
                 else{
                     return ko.toJSON(alm.PassingYear) == ko.toJSON(self.AluminiList.PassingYear());
                 }
             });
         }
     }); 
 

    $.ajax({
        type: "POST",
        url: "/WebServices/DataService.asmx/GetAllStreams",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
        var streams = $.map(data.d, function (item) {
            return new BindStreams(item)
        });
        self.Streams(streams);
        }
    });
}

function BindStreams(data) {
    this.StreamID = ko.observable(data.StreamID);
    this.Stream = ko.observable(data.Stream);
}